<template>
  <div id='box'>
    <div id='con1'>
      <p
        class="con-p"
        ref='conp'
        v-for='(item,index) in items'
        :key='index'
        :style="{animation:'roll' +' ' + items.length*2 + 's' +' ' + 'linear infinite normal'}"
      >{{item.userName}}: {{item.blessing}}</p>
    </div>
  </div>
</template>

<script>
import { onMounted, ref } from "vue";
export default {
  name: "BlessingRoll",
  setup() {
    const items = ref([
      { userName: "老熊", blessing: "新婚快乐" },
      { userName: "小段", blessing: "新婚快乐" },
      { userName: "老王", blessing: "新婚快乐，百年好合" },
      { userName: "文姬", blessing: "新婚快乐" },
      { userName: "小张", blessing: "新婚快乐" },
      { userName: "大毛", blessing: "新婚快乐" },
      { userName: "老二", blessing: "新婚快乐" },
      { userName: "文姬", blessing: "新婚快乐" },
      { userName: "小张", blessing: "新婚快乐" },
      { userName: "大毛", blessing: "新婚快乐" },
      { userName: "老二", blessing: "新婚快乐" },
      { userName: "文姬", blessing: "新婚快乐" },
      { userName: "小张", blessing: "新婚快乐" },
    ]);
    const conp = ref(null);
    const addKeyFrames = (y) => {
      var style = document.createElement("style");
      style.setAttribute("type", "text/css");
      document.head.appendChild(style);
      var keyFrames =
        "\
        @keyframes roll {\
            0% {\
                transform: translate3d(0, 0, 0);\
            }\
            100% {\
                transform: translate3d(0, A_DYNAMIC_VALUE, 0);\
            }\
        }";
      style.innerHTML = keyFrames.replace(/A_DYNAMIC_VALUE/g, y);
    };
    const rollInit = () => {
      var height = items.value.length * 30;
      addKeyFrames("-" + height + "px");
      conp.value.className = "con-p roll";
    };
    onMounted(() => {
      rollInit();
    });
    return {
      items,
      addKeyFrames,
      rollInit,
      conp,
    };
  },
};
</script>

<style scoped lang='scss'>
// 去掉滚动条的默认样式
::-webkit-scrollbar {
  width: 0;
  height: 0;
}
#box {
  position: absolute;
  bottom: 60px;
  padding-left: 15px;
  height: 90px;
  width: auto;
  line-height: 20px;
  overflow: hidden;
}
.con-p {
  height: 20px;
  padding: 0px 10px;
  margin-top: 10px;
  border-radius: 10px;
  background: #333;
  opacity: 0.5;
  color: #fff;
}
</style>
